package com.nlscan.ncomgateway.core.sdk.http

import com.nlscan.ble.NlsBleDevice
import com.nlscan.ble.util.NLogUtil
import com.nlscan.ncomgateway.core.common.AppStateHelper
import com.nlscan.ncomgateway.core.data.preference.SettingsPreferenceManager
import com.nlscan.ncomgateway.core.model.entity.getAliasName
import com.nlscan.ncomgateway.core.newland.entity.NlsIpCommand
import com.nlscan.ncomgateway.core.newland.helper.NlsIpCommandHelper
import com.nlscan.scanner.entity.NlsScanResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * <pre>
 *  author: lin.qing
 *  time  : 2023/6/19
 *  desc  :
 * </pre>
 */
object HttpManager {

    fun sendScanData(scanResult: NlsScanResult, bleDevice: NlsBleDevice,serialNumber:String) {
        val requestMethod = SettingsPreferenceManager.getHttpOutputRequestMethod()
        val requestPathParam = getCorrectRequestPathParam()
        val barcodeField = SettingsPreferenceManager.getDataFormatBarcodeField()
        val deviceNameField = SettingsPreferenceManager.getDataFormatDeviceNameField()
        val deviceAddressField = SettingsPreferenceManager.getDataFormatDeviceAddressField()
        val serialNumberField = SettingsPreferenceManager.getDataFormatSerialNumberField()
        val barcodeTypeField = SettingsPreferenceManager.getDataFormatBarcodeTypeField()
        val barcodeTypeNameField = SettingsPreferenceManager.getDataFormatBarcodeTypeNameField()
        val queryMap = hashMapOf<String, String>(
            barcodeField to scanResult.barcodeTypeName, deviceNameField to bleDevice.getAliasName(),
            deviceAddressField to bleDevice.address,
            serialNumberField to serialNumber,
            barcodeTypeField to scanResult.barcodeType.toString(),
            barcodeTypeNameField to scanResult.barcodeTypeName
        )
        NLogUtil.i("HttpManager sendScanData requestMethod: $requestMethod," +
                " getCorrectRequestPathParam: $requestPathParam")
        GlobalScope
            .launch(Dispatchers.Main) {
            try {

                var result : NlsIpCommand? = null

                withContext(Dispatchers.IO) {
                    result = if (AppStateHelper.isHttpGetRequest(requestMethod)) {
                        RetrofitClient.getHttpOutputApi()
                            ?.sendScanDataByGet(requestPathParam, queryMap)
                    } else {
                        RetrofitClient.getHttpOutputApi()
                            ?.sendScanDataByPost(requestPathParam, queryMap)
                    }

                    NLogUtil.i("sendScanData result: $result")
                }
                // 回到主线程发送命令
                NlsIpCommandHelper.sendIpCommand(result)

            } catch (e: Exception) {
                NLogUtil.e("sendScanData failed! e: ${e.message}")
            }
        }
    }


    private fun getCorrectRequestPathParam():String {
        var requestPathParam = SettingsPreferenceManager.getHttpOutputRequestPathParam()
        //fix:ipsapi/api/v1.0/Scans/add?
        requestPathParam = requestPathParam.removeSuffix("?")
        requestPathParam = requestPathParam.removeSuffix("/")
        return requestPathParam
    }

}